﻿using KeepAnimals.DAL;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KeepAnimals.BLL
{
    public class Department
    {
        public const int BOX_EMPTY = 0;
        public const int BOX_FULL = 1;

        public int Id { get; set; }
        public int Empty { get; set; }


        public Department()
        {
        }

        //Fonction pour ajouter un animal dans la base de donnée depuis le formulaire de la vue, elle renvoi l'id du dernier animal mis dans la base. 
        public int AddDepartmentInDatabase()
        {
            MySqlCommand command = Connection.getInstanceOfCommand();
            //MySqlCommand command = Connection.connection.CreateCommand();

            command.CommandText = "INSERT INTO departments(Empty)VALUES(?Empty);";
            command.Parameters.AddWithValue("?Empty", this.Empty);
            Connection.AddUpdateCommandInDatabase(command);
            return Connection.ReturnLastId(Connection.TABLE_NAME_DEPARTMENT);
        }
        /// <summary>
        /// Updates the animal in database.
        /// </summary>
        public void UpdateDepartmentInDatabase()
        {
            MySqlCommand command = Connection.getInstanceOfCommand();
            //MySqlCommand command = Connection.connection.CreateCommand();

            command.CommandText = "UPDATE departments SET Id = ?Id,Empty =?EmptyWHERE id ="+this.Id+";";
            command.Parameters.AddWithValue("?Id", this.Id);
            command.Parameters.AddWithValue("?Empty", this.Empty);
          
            Connection.AddUpdateCommandInDatabase(command);
        }
        /// <summary>
        /// Gets the animal by identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static Department GetDepartmentById(int id)
        {
            
            Connection.OpenConnection();
            Department tempDepartment = new Department();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM departments WHERE Id = "+id+";";
            MySqlDataReader rdr = Connection.getAllData(command);
            try
            {
                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(0))
                        tempDepartment.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempDepartment.Empty = rdr.GetInt32(1);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return tempDepartment;
        }

           

        //Fonction pour envoyer a la vu la liste de l'animal selection , exemple chat / chien , on passe un id de la categorie animal
        public static  List<Department> GetListOfAllDepartments()
        {
            Connection.OpenConnection();
            List<Department> departments = new List<Department>();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM Departments";
            MySqlDataReader rdr = Connection.getAllData(command);

             try
            {
                while (rdr.Read())
                {
                    Department tempDepartment = new Department();
                    if (!rdr.IsDBNull(0))
                        tempDepartment.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempDepartment.Empty = rdr.GetInt32(1);
                    departments.Add(tempDepartment);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }
            return departments;
        }

    }
}

